/** 
 * ================================================================================
 * 		Project		: Kompas Bola
 * 		File		: CommentActivity.java
 * 		Description	: Comment Screen
 * 		Called		: DetailActivity.java
 * ================================================================================
 * */
package com.kompas.bola.application;

import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import java.util.ArrayList;
import android.app.Activity;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.Toast;
import android.graphics.Color;
import android.content.Intent;
import com.kompas.bola.application.R;
import android.graphics.drawable.ColorDrawable;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.kompas.bola.application.adapter.ListCommentAdapter;
import com.kompas.bola.application.analytics.FlurryUtil;
import com.kompas.bola.application.analytics.GAnalyticsUtil;
import com.kompas.bola.application.dao.Dao;
import com.kompas.bola.application.model.ContentDetail;
import com.kompas.bola.application.ui.ListUI;
import com.kompas.bola.application.ui.ListUI.OnRefreshListener;
import com.kompas.bola.application.utils.Constants;
/** =============================================================================== */
public class CommentActivity extends Activity{
	private String guid;
	private ListUI list;
	private ListCommentAdapter adapter;
	private List<ContentDetail> list_data			= new ArrayList<ContentDetail>();
	private WebView imgProgressBar;
	private HashMap<String, String> value;
	/** =========================================================================== */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_comment_activity);		
		overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
		getActionBar().setLogo(getResources().getDrawable(R.drawable.logo));
		getActionBar().setTitle(Constants.COMMENT_SCREEN);
		
		Intent in 									= getIntent();
		guid 										= in.getStringExtra(Constants.GUID);   

		list										= (ListUI) findViewById(R.id.list_comment);
		adapter										= new ListCommentAdapter(CommentActivity.this, list_data);

		imgProgressBar								= (WebView) findViewById(R.id.imgProgressBarComment);

		String path									= "file:///android_asset/loading.html";
		imgProgressBar.loadUrl(path);	
		imgProgressBar.getSettings().setDefaultZoom(ZoomDensity.FAR);
		imgProgressBar.setVisibility(View.VISIBLE);

		/** 
		 * START GOOGLE ANALYTICS */
		value													= new HashMap<String, String>();
		value.put(Fields.HIT_TYPE, "appview");
		value.put(Fields.SCREEN_NAME, Constants.COMMENT_SCREEN);
		GAnalyticsUtil.startSessionWithGA(this,value);
		
		/** 
		 * Load Comment Task */
		new LoadComment().execute(guid);
		
		/**
		 * Pull Refresh Listener */
		list.setOnRefreshListener(new OnRefreshListener() {
			
			public void onRefresh() {
				list.prepareForRefresh();
				new LoadComment().execute(guid);
			}
		});
	}
	/** =========================================================================== */
	private class LoadComment extends AsyncTask<String, Void, List<ContentDetail>>{
		@Override
		protected List<ContentDetail> doInBackground(String... params) {
			// TODO Auto-generated method stub
			String guid								= params[0];
			return Dao.getListComment(guid, 1);
		}
		
		protected void onPostExecute(
				List<ContentDetail> result){
			if (!isCancelled()){				
				if(result != null){
					imgProgressBar.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
					
					try {
						adapter						= new ListCommentAdapter(CommentActivity.this, result);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						cancel(true);
					}
					
					adapter.notifyDataSetChanged();
					list.setAdapter(adapter);	
					list.resetHeader();
					list.onRefreshComplete();	
				}else{
					imgProgressBar.setVisibility(View.GONE);
					Toast.makeText(CommentActivity.this, Constants.NO_COMMENT, Toast.LENGTH_LONG).show();
					
					adapter.notifyDataSetChanged();
				}				
			}else
				return;	
		}	
	}	
	/** =========================================================================== */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}	
	/** =========================================================================== */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        	Intent main						= new Intent(CommentActivity.this, MainActivity.class);
            startActivity(main);
			overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }	
	/** =========================== s : Google Analytics ========================== */
	@Override
	public void onStart() {
		super.onStart();		
		
		EasyTracker.getInstance(this).activityStart(this);  // Add this method.		
	    FlurryUtil.startSessionWithFlurry(this, Constants.COMMENT_SCREEN);
	}
	/** =========================================================================== */
	@Override
	public void onStop() {
		super.onStop();
		
		new LoadComment().cancel(true);
	    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
	    FlurryUtil.endSessionWithFlurry(this,  Constants.COMMENT_SCREEN);
	}
	/** =========================== e : Google Analytics ========================== */
}
